// mobile bind
// ============

define(['zepto', 'sui', 'zData', 'jrange', 'qrcode'], function($){
  return {
    init: function(data) {
      if(location.href.indexOf("#reloaded")==-1){
        location.href=location.href+"#reloaded";
        location.reload();
      }
      
      this.initRange();
      this.initSubmit(data.secondaryAgentId);

    },
    initRange: function() {
      $('.single-slider').jRange({
          from: 0,
          to: 10,
          step: 1,
          format: function(value, pointer) {
            return (Math.round(value) + "%");
          },
          width: 170,
          theme: 'theme-blue',
          showLabels: true,
          snap: true
      });
      
      $(document).on("click", ".iconfont-minus", function() {
    	var recruitVal = Number($(".single-slider").val()) < 1 ? '1' : Number($(".single-slider").val()) - 1
        $('.single-slider').jRange('setValue', recruitVal.toFixed(0));
      })
      
      $(document).on("click", ".iconfont-plus", function() {

    	var recruitVal = Number($(".single-slider").val()) > 10 ? '10' : Number($(".single-slider").val()) + 1
    	$('.single-slider').jRange('setValue', recruitVal.toFixed(0));
      })

    },

    initSubmit: function (secondaryAgentId) {
      $(document).on("click", ".create-button", function() {
        var recruitVal = Number($(".single-slider").val())
        $.ajax({
          url: window.__CTX__.root + '/profile/secondary-agents/' + secondaryAgentId + '/edit-commission-rate',
          type: 'POST',
          dataType: 'json',
          data: {
            rate: recruitVal,
            _method: 'PATCH'
          },
          success: function(res) {
            if (res.code === '0') {
              $.toast(res.message || "操作成功！");
            }else{
              $.toast(res.message || "操作失败！");
            }
            window.location.href = window.__CTX__.domain + "/profile/secondary-agents"
          },
          fail: function(res) {
            debugger
            $.toast(res.message || "操作失败！");
          }
        });
      })
    }

  }
})